using System;

using UnityEngine;
using UnityEngine.Playables;

public class MoveBackPlayerPlayableBehaviour : PlayableBehaviour
{
    protected Transform m_player;
    protected Vector3 m_startPos;
    public static Vector3 m_endPos;
    protected Vector3 m_forward;
    protected float m_time;
    protected float m_durection;

    public void Initialize(Transform transform)
    {
        this.m_player = transform;
    }

    public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    {
        m_time += info.deltaTime;
        if (Application.isPlaying)
            m_player.transform.forward = m_forward;
        m_player.transform.position = Vector3.Lerp(m_startPos, m_endPos, m_time / (float)m_durection);
    }

    public override void OnBehaviourPlay(Playable playable, FrameData info)
    {
        base.OnBehaviourPlay(playable, info);
        this.m_startPos = m_player.position;
        m_forward = m_endPos - m_startPos;
        m_forward.y = 0;
        m_durection = (float)playable.GetDuration();
    }
}
